package com.cow.framework.core.config.dict;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

import com.cow.framework.core.cache.RedisUtil;

/**
 * 系统枚举字典获取工具
 * 
 *  @author yangsj
 */
@Component
public class DictUtil {
	@Autowired
	private RedisUtil redisUtil;
	public Map getDict(String dictName){
		Map<String,Map<String,String>> sys_dictMap =  (Map<String,Map<String,String>>)redisUtil.get("sys_dictMap");	
		return sys_dictMap.get(dictName);
	}	
	
	public Dict getDict(String dictName,String value){
		Map<String,Map<String,Dict>> sys_dictMap =  (Map<String,Map<String,Dict>>)redisUtil.get("app_dictMap");	
		return sys_dictMap.get(dictName).get(value);
	}
	
	public String getDictValueName(String dictName,String value){
		Map<String,Map<String,Dict>> sys_dictMap =  (Map<String,Map<String,Dict>>)redisUtil.get("app_dictMap");	
		if(sys_dictMap.get(dictName).get(value) == null){
			return "";	
		}
		return sys_dictMap.get(dictName).get(value).getValueName();
	}
	
	public void valueToName(Map map,String... keys){
		for(String key:keys){
			if(map.get(key)!=null){
				map.put(key, getDictValueName(key, (String)map.get(key)));
			}
		}
	}
			
	/**对Map对象里的属性批量转义*/
	public void valueToName(Map beanMap,Map dictMap){
		Map<String,Map<String,Dict>> sys_dictMap =  (Map<String,Map<String,Dict>>)redisUtil.get("app_dictMap");	
		for(Object o:dictMap.keySet()){
			String key = (String)o;
			String dictKey = (String)dictMap.get(key);
			if(beanMap.get(key)!=null){
				Dict dict = sys_dictMap.get(dictKey).get(beanMap.get(key).toString());
				String valueName = dict == null? "":dict.getValueName();
				beanMap.put(key+"Name", valueName);
			}
		}
	}
	
	/**获取枚举列表*/
	public Map getDicts(@RequestParam String dictNames) throws Exception{
		String[] dictNameArray = dictNames.split(","); 
		Map dictMaps = new HashMap();
		Map<String,Map<String,Dict>> sys_dictMap =  (Map<String,Map<String,Dict>>)redisUtil.get("app_dictMap");	
		for(String key:dictNameArray){
			Map dictMap =  sys_dictMap.get(key);
			dictMaps.put(key, dictMap);
		}
		return dictMaps;
	}
	
	/**获取枚举列表*/
	public Map getDictValueNames(@RequestParam String dictNames) throws Exception{
		String[] dictNameArray = dictNames.split(","); 
		Map dictMaps = new HashMap();
		Map<String,Map<String,String>> sys_dictMap =  (Map<String,Map<String,String>>)redisUtil.get("sys_dictMap");	
		for(String key:dictNameArray){
			Map dictMap =  sys_dictMap.get(key);
			dictMaps.put(key, dictMap);
		}
		return dictMaps;
	}
}
